<?php
	

		class shareAction extends userAction{	
			protected function _initialize() {
				parent::_initialize();
			}
			
			/**
			 * 获得分享列表(所有好友/某个好友/自己)
			 *
			 */
			public function index(){
				import("ORG.Util.Page"); //导入分页类
				isset($_GET["u"]) ? $u = safeRequest($_GET["u"]) : $u= "all";    //条件参数
				isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;       //页数
				$dao = D("shars");
				if("all" == $u){ //所有好友的分享
					$friendIds 	= 	getFriendList($this->userInfo['id']);
					$shareCount = $dao->count("userid in ($friendIds)","id"); //获得好友的分享记录总数
					$listRows = C("publicListRows"); 
					$p = new Page($shareCount,$listRows); 
					$p->setConfig('header' ,'个分享 '); 
					$shareList	= $dao->findAll("userid in ($friendIds)","*",'regtime desc',$p->firstRow.','.$p->listRows);//获得好友的分享记录记录
					$page  = $p->show();
					
					//绑定输出
					$this->assign("shareList",$shareList);
					$this->assign("page",$page);
					$this->display();
				}else{
					$this->rank =  getRank($this->userInfo['id'],$u,"share");
					if(!$this->rank){
						redirect(__APP__."/rank/norank/u/".$u);
						return false;
					}
					$shareCount = $dao->count("userid=".$u,"id"); //获得相关share记录总数
					$listRows = C("publicListRows"); //从配置文件中读取mini博客每页显示记录数
					$p = new Page($shareCount,$listRows); //实例化分页类
					$p->setConfig('header' ,'个分享 '); 
					$shareList	= $dao->findAll("userid=".$u,"*",'regtime desc',$p->firstRow.','.$p->listRows);//获得相关迷你博客记录
					$page  = $p->show();
					if($u != $this->userInfo['id']){
						$userInfo = D("user")->find("id=".$u,"id,username");
						$this->assign("userInfo",$userInfo);	
					}
					$this->assign("myid",$this->userInfo['id']);
					$this->assign("shareList",$shareList);
					$this->assign("page",$page);
					$this->display("my");
				}
			}
			
			//添加分享
			public function addshare(){
				$g = $_GET["g"];
				switch ($g){
					case "home"://分享用户
						$u = safeRequest($_GET["u"]);
						$record = D("user")->find("id=".$u,"username,network_city");
						$this->assign("record",$record);
						isset($_GET["ajax"]) ? $this->display("home4ajax") : $this->display("home");
						break;
					case "blogs": //他享博客
						$id = safeRequest($_GET["id"]);
						$record = D("blogs")->find("id=".$id,"title,userid,content");
						$this->assign("record",$record);
						isset($_GET["ajax"]) ? $this->display("blog4ajax") : $this->display("blog");
						break;
					case "album"://分享一个相册
						$id = safeRequest($_GET["id"]);
						$record = D("albums")->find("id=".$id,"id,userid,name,defaultphoto");
						$this->assign("record",$record);
						isset($_GET["ajax"]) ? $this->display("album4ajax") : $this->display("album");
						break;
					case "photo": //分享相片
						isset($_GET["ajax"]) ? $this->display("photo4ajax") : $this->display("photo");
						break;
					case "group": //分享群组
						$id = safeRequest($_GET["id"]);
						$record = D("groups")->find("id=".$id,"id,name,img");
						$this->assign("record",$record);
						isset($_GET["ajax"]) ? $this->display("group4ajax") : $this->display("group");
						break;
					case "topic"://分享话题
						$id = safeRequest($_GET["id"]);
						$record = D("groups_topics")->find("id=".$id,"id,title,content");
						$this->assign("record",$record);
						isset($_GET["ajax"]) ? $this->display("topic4ajax") : $this->display("topic");
						break;
					default: //分享一个网址
						isset($_GET["ajax"]) ? $this->display("url4ajax") : $this->display("url");
						break;
				}
			}
			
			//保存分享
			public function saveshare(){
				$dao = D("shars");
				$dao->userid = $this->userInfo['id'];
				$dao->module = $_POST["g"];
				$dao->moduleid = $_POST["moduleid"];
				$dao->shareurl = $_POST["url"];
				$dao->desc = $_POST["sharedesc"];
				$dao->regtime = time();
				$id= $dao->add();
				if($id>0){
					D("cache")->setField("shareCount","(shareCount+1)","userid=".$this->userInfo['id']);
					addFeed($this->userInfo['id'],"share",$dao->module,$dao->moduleid,$id);
					isset($_POST["ajax"]) ? print "success" : $this->success("保存成功");
				}else{
					isset($_POST["ajax"]) ? print "error" : $this->error("保存失败，你可以重试");
				}
			}

			//删除分享
			public function deleteshare(){
				$id = safeRequest($_GET["id"]);
				if(D("shars")->delete("id=".$id." and userid=".$this->userInfo['id'])){
					D("shars_reverts")->delete("shareid=".$id);//删除评论
					D("cache")->setField("shareCount","(shareCount-1)","userid=".$this->userInfo['id']);
					delFeed(0,$this->userInfo['id'],"share",$id,$id);//删除动态
					isset($_GET["ajax"]) ? print "success" : $this->success("操作成功");
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("操作失败");
				}
			}

			//分享回复列表
			public function revert(){
				$id = safeRequest($_GET["id"]);
				$list = D("shars_reverts")->findAll("shareid=".$id);
				$this->assign("list",$list);
				$this->display();
			}

			//添加回复
			public function addrevert(){
				$shareid = safeRequest($_POST["id"]);
				$dao = D("shars_reverts");
				$dao->shareid= $shareid;
				$dao->userid = $this->userInfo['id'];
				$dao->content = htmlspecialchars($_POST["content"],ENT_QUOTES);
				$dao->regtime = time();
				$id = $dao->add();
				if($id>0){
					D("shars")->setField("revertnum","(revertnum+1)","id=".$shareid);
					isset($_POST["ajax"]) ? print "success" : $this->success("评论成功");
				}else{
					isset($_POST["ajax"]) ? print "error" : $this->error("评论失败");
				}

			}

			//删除回复
			public function deleterevert(){
				$id = safeRequest($_GET["id"]);
				if(D("shars_reverts")->delete("id=".$id." and userid=".$this->userInfo['id'])){
					$shareid = safeRequest("sid");
					D("shars")->setField("revertnum","(revertnum-1)","id=".$shareid);
					isset($_GET["ajax"]) ? print "success" : $this->success("操作成功");
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error("操作失败");
				}
			}
		}
?>